package com.yunban.project.message.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yunban.project.message.domain.BusinessCard;
import com.yunban.project.message.dto.BusinessCardReq;
import com.yunban.project.message.mapper.BusinessCardMapper;
import com.yunban.project.message.resp.BusinessCardResp;
import com.yunban.project.message.service.BusinessCardService;
import net.lab1024.sa.base.common.exception.BusinessException;
import net.lab1024.sa.base.common.util.SmartPageUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * Description:
 * Author:han
 * Date: 2024-05-21
 */
@Service
public class BusinessCardServiceImpl extends ServiceImpl<BusinessCardMapper, BusinessCard>implements BusinessCardService {
    @Override
    public Boolean saveBusinessCard(BusinessCardReq businessCardReq) {
        //保存到数据库
        BusinessCard businessCard = new BusinessCard();
        BeanUtils.copyProperties(businessCardReq, businessCard);
        return this.save(businessCard);
    }

    @Override
    public Boolean updateBusinessCard(Long id,BusinessCardReq businessCardReq) {
        BusinessCard businessCard = this.getById(id);
        if(ObjectUtils.isEmpty(businessCard)){
            throw new BusinessException("名片不存在");
        }
        BeanUtils.copyProperties(businessCardReq, businessCard);
        return this.updateById(businessCard);
    }

    @Override
    public Boolean remove(Long id) {
        BusinessCard businessCard = this.getById(id);
        if(ObjectUtils.isEmpty(businessCard)){
            throw new BusinessException("名片不存在");
        }
        return this.removeById(id);
    }

    @Override
    public List<BusinessCardResp> getBusinessCardList() {
        List<BusinessCard> businessCards = this.list();
        return SmartPageUtil.convert(businessCards, BusinessCardResp.class);
    }
}
